package swu.cx.test

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View

class MouseLoading:View{
    //圆的半径
    private var radius =0f
    //大圆的半径
    private var mouseRadius=0f
    //两者之间的间距
    private var space=0f

    //大圆中心点坐标
    private var cx=0f
    private var cy=0f
    //张开闭合角度
    private var mouseAngle=0f
    //保存值变化动画
    private var mouseAnim:ValueAnimator?=null
    //记录球的变化因子
    private var ballMoveX=0f
    private var ballAnim:ValueAnimator?=null
    //动画集合
    private var togetherAnim=AnimatorSet()
    //画笔
    private val mArcPaint: Paint by lazy {
        Paint().apply {
            color=Color.CYAN
            style= Paint.Style.FILL
        }
    }
    //代码创建
    constructor(context: Context):super(context){}
    //xml创建
    constructor(context: Context,attriset:AttributeSet):super(context,attriset){}
    //有样式的
    constructor(context: Context,attriset:AttributeSet,style:Int):super(context,attriset,style){}

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        //确定半径
        if (width>=height){
            radius=height/6f
            //如果宽度小于8.5个R
            if (8.5*radius>width){
                radius=width/8.5f
            }
        }else{
            radius=width/8.5f
            //如果高度小于6个R
            if (6*radius>height){
                radius=height/6f
            }
        }
        mouseRadius=3f*radius
        space=0.5f*radius
        cx=(width-8.5f*radius)/2f+mouseRadius
        cy=height/2f
    }
    override fun onDraw(canvas: Canvas?) {
        canvas?.drawArc(
            cx-mouseRadius,cy-mouseRadius,cx+mouseRadius,cy+mouseRadius,
            mouseAngle,360f-2*mouseAngle,true,mArcPaint
        )
        //绘制圆形
        canvas?.drawCircle(cx+4.5f*radius-ballMoveX,cy,radius,mArcPaint)
    }
    fun createAnim(){
        if (mouseAnim==null) {
            mouseAnim = ValueAnimator.ofFloat(0f, 45f, 0f).apply {
                duration = 500
                repeatCount = ValueAnimator.INFINITE
                addUpdateListener {
                    mouseAngle = it.animatedValue as Float
                    invalidate()
                }
            }
        }
        if (ballAnim==null){
            ballAnim=ValueAnimator.ofFloat(0f,4.5f*radius).apply {
                duration=500
                repeatCount=ValueAnimator.INFINITE
                addUpdateListener {
                    ballMoveX=it.animatedValue as Float
                    invalidate()
                }
            }
        }
        togetherAnim.playTogether(mouseAnim,ballAnim)
    }
    //开始动画
    fun startAnim(){
        createAnim()
        //判断当前的状态
      if (togetherAnim.isPaused!!){
          togetherAnim.resume()
      }else{
          togetherAnim.start()
      }
    }
    //结束动画
    fun stopAnim(){
        togetherAnim.pause()
    }
}